/**
 * @description 微博 @ controller
 * @author chengpeng
 */

const {
    SuccessModel,
    ErrorModel
} = require('../model/ResModel')
const {
    getAtRelationCount,
    getAtUserBlogList
} = require('../services/atRelation')
const {
    PAGE_SIZE
} = require('../config/constant')
const {
    updateAtRelation
} = require('../services/atRelation')
/**
 * @description 获取 @ 我的数量
 * @author chengpeng
 * @param {number} userId 
 */
async function getAtMeCount(userId) {
    const count = await getAtRelationCount(userId)
    return new SuccessModel({
        count
    })
}

/**
 * @description 获取atMe微博列表
 * @author chengpeng
 * @param {number} userId 
 * @param {number} pageIndex 
 */
async function getAtMeBlogList(userId, pageIndex = 0) {
    const result = await getAtUserBlogList({
        userId,
        pageIndex,
        pageSize: PAGE_SIZE
    })

    const {
        count,
        blogList
    } = result

    return new SuccessModel({
        isEmpty: blogList.length === 0,
        blogList,
        pageSize: PAGE_SIZE,
        pageIndex,
        count
    })
}

/**
 * @description 标记为已读
 * @author chengpeng
 * @param {number} userId 
 */
async function markAsRead(userId) {
    try {
        await updateAtRelation({
            newIsRead: true
        }, {
            userId,
            isRead: false
        })
    } catch (error) {
        console.error(error)
    }
}

module.exports = {
    getAtMeCount,
    getAtMeBlogList,
    markAsRead
}